package com.jhd.fuzhou.util;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.FuzhouApplication;
import com.jhd.fuzhou.service.SupervisionService;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.SupervisionInspectionVo;
import com.jhd.fuzhou.vo.Upload;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class UploadSign {
    public static void uploadSign(FuzhouApplication application, Context context,SupervisionInspectionVo supervisionInspectionVo, int position, String type, File file){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.UC());//URL的头
        SupervisionService vs = retrofit.create(SupervisionService.class);
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        params.put("isPublic",false);
        RequestBody fileRQ=RequestBody.create(MediaType.parse("multipart/form-data"), file);
        String ss=file.getName();
        MultipartBody.Part part=MultipartBody.Part.createFormData("file", file.getName(), fileRQ);
        Call<JsonForResult> jsonForResultCall = vs.uploadFileByPos(application.getAccessToken(),params,part);
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    if(response.code()==200) {
                        JsonForResult jsonForResult = (JsonForResult)response.body();
                        if (jsonForResult != null && jsonForResult.isSuccess()) {
                            if(position==1){
                                supervisionInspectionVo.setUpload1(JSON.parseObject(jsonForResult.getData().toString(), Upload.class));
                            }else if(position==2){
                                supervisionInspectionVo.setUpload2(JSON.parseObject(jsonForResult.getData().toString(), Upload.class));
                            }else if(position==3){
                                supervisionInspectionVo.setUpload3(JSON.parseObject(jsonForResult.getData().toString(), Upload.class));
                            }else if(position==4){
                                supervisionInspectionVo.setUpload4(JSON.parseObject(jsonForResult.getData().toString(), Upload.class));
                            }
                        }
                    }
                } catch (Exception e) {
                    UtilHelper.alertMsg(context, "错误", e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "上传签名失败!";
                //UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    public static void uploadPhoto(FuzhouApplication application, Context context,SupervisionInspectionVo supervisionInspectionVo, int position, String type, File file){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.UC());//URL的头
        SupervisionService vs = retrofit.create(SupervisionService.class);
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        params.put("isPublic",false);
        RequestBody fileRQ=RequestBody.create(MediaType.parse("multipart/form-data"), file);
        String ss=file.getName();
        MultipartBody.Part part=MultipartBody.Part.createFormData("file", file.getName(), fileRQ);
        Call<JsonForResult> jsonForResultCall = vs.uploadFileByPos(application.getAccessToken(),params,part);
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    if(response.code()==200) {
                        JsonForResult jsonForResult = (JsonForResult)response.body();
                        if (jsonForResult != null && jsonForResult.isSuccess()) {
                            if(position==5){
                                supervisionInspectionVo.setUpload5(JSON.parseObject(jsonForResult.getData().toString(), Upload.class));
                            }
                        }
                    }
                } catch (Exception e) {
                    UtilHelper.alertMsg(context, "错误", e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "上传图片证据失败!";
                UtilHelper.alertMsg2(context, "错误", msg);//图片上传失败回到上个页面
            }
        });
    }
}
